package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername()throws Exception{
        try{
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/findHealthManager")
    public Result findHealthManager(){
        try {
            List<com.itheima.pojo.User> user = userService.findHealthManager();
            return new Result(true , MessageConstant.GET_HEALTHMANAGE_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false , MessageConstant.GET_HEALTHMANAGE_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {

            userService.add(user, roleIds);
        }catch (Exception e){
            //新增失败
            return new Result(false, "新增失败");
        }
        //新增成功
        return new Result(true, "添加成功");
    }

    @RequestMapping("/findById")
    public Result findById( int id){
        com.itheima.pojo.User user = userService.findById(id);
        if(user != null){
            Result result = new Result(true, "查询成功",user);
            return result;
        }
        return new Result(false,"查询失败");
    }
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId( int id){
        try{
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, "查询成功", roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[]roleIds, @RequestBody com.itheima.pojo.User user){

        try {
            userService.edit(user,roleIds);
        }catch (Exception e){
            return new Result(false, "修改失败");
        }
        return new Result(true, "修改成功");
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
        }catch (Exception e){
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除成功");
    }
}
